﻿using DataAccess.PersonalBusinessLoan;
using FOModels.PersonalBusinessLoan;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Bussiness.PersonalBusinessLoan
{
    public class PersonalBusinessLoanBLL
    {
        private PersonalBusinessLoanDAL dal;
        public PersonalBusinessLoanBLL()
        {
            dal = new PersonalBusinessLoanDAL();
        }
        public List<PersonalBusinessLoanApplyModels> getcontract()
        {
            List<PersonalBusinessLoanApplyModels> list = new List<PersonalBusinessLoanApplyModels>();
            PersonalBusinessLoanApplyModels fur;
            DataTable tb = dal.getcontract();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new PersonalBusinessLoanApplyModels();
                    fur.PersonalBusinessLoanApplyID = int.Parse(row[0].ToString());
                    fur.PersonalBusinessLoanID = int.Parse(row[1].ToString());
                    fur.CustomerName = row[2].ToString();
                    fur.CustomerEmail = row[3].ToString();
                    fur.CustomerPhone = row[4].ToString();
                    fur.CustomerIncome = row[5].ToString();
                    fur.CustomerDOB = row[6].ToString();
                    fur.StatusID = int.Parse(row[7].ToString());
                    fur.AppDate = row[8].ToString();
                    fur.PersonalBusinessLoanName = row[10].ToString();
                    fur.StatusName = row[17].ToString();
                    fur.CompanyImg = row[19].ToString();
                    fur.CompanyName = row[26].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<PersonalBusinessLoanApplyModels> getcontractbystatuscount(int statusid)
        {
            List<PersonalBusinessLoanApplyModels> list = new List<PersonalBusinessLoanApplyModels>();
            PersonalBusinessLoanApplyModels fur;
            DataTable tb = dal.getcontractbystatuscount(statusid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new PersonalBusinessLoanApplyModels();
                    fur.PersonalBusinessLoanApplyID = int.Parse(row[0].ToString());
                    fur.PersonalBusinessLoanID = int.Parse(row[1].ToString());
                    fur.CustomerName = row[2].ToString();
                    fur.CustomerEmail = row[3].ToString();
                    fur.CustomerPhone = row[4].ToString();
                    fur.CustomerIncome = row[5].ToString();
                    fur.CustomerDOB = row[6].ToString();
                    fur.StatusID = int.Parse(row[7].ToString());
                    fur.AppDate = row[8].ToString();
                    fur.PersonalBusinessLoanName = row[10].ToString();
                    fur.StatusName = row[17].ToString();
                    fur.CompanyImg = row[19].ToString();
                    fur.CompanyName = row[26].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public PersonalBusinessLoanApplyModels getcontractbyid(int PersonalBusinessLoanapplyid)
        {

            PersonalBusinessLoanApplyModels fur = new PersonalBusinessLoanApplyModels();
            DataTable tb = dal.getcontractbyid(PersonalBusinessLoanapplyid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new PersonalBusinessLoanApplyModels();
                    fur.PersonalBusinessLoanApplyID = int.Parse(row[0].ToString());
                    fur.PersonalBusinessLoanID = int.Parse(row[1].ToString());
                    fur.CustomerName = row[2].ToString();
                    fur.CustomerEmail = row[3].ToString();
                    fur.CustomerPhone = row[4].ToString();
                    fur.CustomerIncome = row[5].ToString();
                    fur.CustomerDOB = row[6].ToString();
                    fur.StatusID = int.Parse(row[7].ToString());
                    fur.AppDate = row[8].ToString();
                    fur.PersonalBusinessLoanName = row[10].ToString();
                    fur.StatusName = row[17].ToString();
                    fur.CompanyImg = row[19].ToString();
                    fur.CompanyName = row[26].ToString();

                }
            }
            return fur;
        }
        public int PersonalBusinessLoanApply(int PersonalBusinessLoanID, PersonalBusinessLoanApplyModels PersonalBusinessLoan)
        {
            if (dal.PersonalBusinessLoanApply(PersonalBusinessLoanID, PersonalBusinessLoan) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int DeletePersonalBusinessLoanApply(int PersonalBusinessLoanApplyID)
        {
            if (dal.DeletePersonalBusinessLoanApply(PersonalBusinessLoanApplyID) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int UpdateStatus(PersonalBusinessLoanApplyModels PersonalBusinessLoan)
        {
            if (dal.UpdateStatus(PersonalBusinessLoan) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public PersonalBusinessLoanModels getTOP1()
        {

            PersonalBusinessLoanModels fur = new PersonalBusinessLoanModels();
            DataTable tb = dal.getTOP1();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {

                    fur.PersonalBusinessLoanID = int.Parse(row[0].ToString());
                }
                return fur;
            }
            else
                return null;
        }
        public List<PersonalBusinessLoanModels> getall(int langid)
        {
            List<PersonalBusinessLoanModels> list = new List<PersonalBusinessLoanModels>();
            PersonalBusinessLoanModels fur;
            DataTable tb = dal.getall(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {

                    fur = new PersonalBusinessLoanModels();
                    fur.PersonalBusinessLoanID = int.Parse(row[0].ToString());
                    fur.PersonalBusinessLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = row[3].ToString();
                    fur.MaximumBalance = row[4].ToString();
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.FlagImage = row[27].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public PersonalBusinessLoanModels getPersonalBusinessLoanByID(int PersonalBusinessLoanID, int langid)
        {

            PersonalBusinessLoanModels fur = new PersonalBusinessLoanModels();
            DataTable tb = dal.getPersonalBusinessLoanByID(PersonalBusinessLoanID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.PersonalBusinessLoanID = int.Parse(row[0].ToString());
                    fur.PersonalBusinessLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                }
                return fur;
            }
            else
                return null;
        }
        public PersonalBusinessLoanModels getPersonalBusinessLoanByIDManager(int PersonalBusinessLoanID, int langid)
        {

            PersonalBusinessLoanModels fur = new PersonalBusinessLoanModels();
            DataTable tb = dal.getPersonalBusinessLoanByIDManager(PersonalBusinessLoanID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.PersonalBusinessLoanID = int.Parse(row[0].ToString());
                    fur.PersonalBusinessLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                }
                return fur;
            }
            else
                return null;
        }
        public List<PersonalBusinessLoanModels> getPersonalBusinessLoanDefault(int langid)
        {
            List<PersonalBusinessLoanModels> list = new List<PersonalBusinessLoanModels>();
            PersonalBusinessLoanModels fur;
            DataTable tb = dal.getPersonalBusinessLoanDefault(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new PersonalBusinessLoanModels();
                    fur.PersonalBusinessLoanID = int.Parse(row[0].ToString());
                    fur.PersonalBusinessLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<PersonalBusinessLoanModels> getPersonalBusinessLoanrelati(int CompanyID, int HomeLoanID, int langid)
        {
            List<PersonalBusinessLoanModels> list = new List<PersonalBusinessLoanModels>();
            PersonalBusinessLoanModels fur;
            DataTable tb = dal.getPersonalBusinessLoanrelati(CompanyID, HomeLoanID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new PersonalBusinessLoanModels();
                    fur.PersonalBusinessLoanID = int.Parse(row[0].ToString());
                    fur.PersonalBusinessLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<PersonalBusinessLoanModels> getPersonalBusinessLoan(int planid, string balance, int years, int langid)
        {
            List<PersonalBusinessLoanModels> list = new List<PersonalBusinessLoanModels>();
            PersonalBusinessLoanModels fur;
            DataTable tb = dal.getPersonalBusinessLoan(planid, balance, years, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new PersonalBusinessLoanModels();
                    fur.PersonalBusinessLoanID = int.Parse(row[0].ToString());
                    fur.PersonalBusinessLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<PersonalBusinessLoanModels> getPersonalBusinessLoanByProvider(int planid, string balance, int years, int CompanyID, int langid)
        {
            List<PersonalBusinessLoanModels> list = new List<PersonalBusinessLoanModels>();
            PersonalBusinessLoanModels fur;
            DataTable tb = dal.getPersonalBusinessLoanByProvider(planid, balance, years, CompanyID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new PersonalBusinessLoanModels();
                    fur.PersonalBusinessLoanID = int.Parse(row[0].ToString());
                    fur.PersonalBusinessLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<PersonalBusinessLoanModels> getPersonalBusinessLoanDefaultByProvider(int CompanyID, int langid)
        {
            List<PersonalBusinessLoanModels> list = new List<PersonalBusinessLoanModels>();
            PersonalBusinessLoanModels fur;
            DataTable tb = dal.getPersonalBusinessLoanDefaultByProvider(CompanyID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new PersonalBusinessLoanModels();
                    fur.PersonalBusinessLoanID = int.Parse(row[0].ToString());
                    fur.PersonalBusinessLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public int UpdatePersonalBusinessLoan(PersonalBusinessLoanModels models, int langid)
        {
            if (dal.UpdatePersonalBusinessLoan(models) > 0 && dal.UpdatePersonalBusinessLoanLang(models, langid) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertPersonalBusinessLoan(PersonalBusinessLoanModels models)
        {
            if (dal.InsertPersonalBusinessLoan(models) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertPersonalBusinessLoanLang(PersonalBusinessLoanModels models, int langid)
        {
            if (dal.InsertPersonalBusinessLoanLang(models, langid) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int DeletePersonalBusinessLoan(int CarLoanID)
        {
            if (dal.DeletePersonalBusinessLoanLang(CarLoanID) > 0 && dal.DeletePersonalBusinessLoan(CarLoanID) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
    }
}
